import 'package:demo_app_video/page/drama_details/drama_details.dart';
import 'package:demo_app_video/page/login.dart';
import 'package:demo_app_video/page/not_fund.dart';
import 'package:demo_app_video/page/personal/about_us.dart';
import 'package:demo_app_video/page/personal/adolescent.dart';
import 'package:demo_app_video/page/personal/history.dart';
import 'package:demo_app_video/page/personal/settings.dart';
import 'package:demo_app_video/page/search/search.dart';
import 'package:demo_app_video/page/search_details/search_details.dart';
import 'package:demo_app_video/widgets/web_view.dart';

import 'routes_handle.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import '../../page/home_main.dart';

/**
 * 路由管理
 */
class Routes {
  static const login = 'login://';
  static const home = 'home://';
  static const search = 'search://';
  static const dramaDetails = 'dramaDetails://';
  static const search_details = "/search_details";
  static const adolescent = "/adolescent";
  static const history = "/history";
  static const about_us = "/about_us";
  static const settings = "/settings";
  static const webview = "/webview";

  Widget _getPage(String url, Map<String, dynamic> params) {
    if (params == null) {
      params = {};
    }

    switch (url) {
      case home:
        return HomeMainPage();
      case webview:
        return WebViewPage(url_text: params['url_text']);
      case settings:
        return SettingsPage();
      case about_us:
        return AboutUsPage();
      case history:
        return HistoryPage();
      case adolescent:
        return AdolescentPage();
      case dramaDetails:
        return DramaDetails(
          drama: params['drama'],
        );
      case login:
        return LoginPage();
      case search:
        return SearchPage();
      case search_details:
        return SearchDetailsPage();

      default:
        return NotFoundPage();
    }
  }

  pushNoParams(BuildContext context, String url,
      {callback, fullscreenDialog = false}) async {
    // url = await Auth().checkPersionMiss(url);
    Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) {
                  return _getPage(url, {});
                },
                fullscreenDialog: fullscreenDialog))
        .then((value) {
      if (callback != null) {
        callback(value);
      }
    });
  }

  push(BuildContext context, String url, dynamic params,
      {callback, fullscreenDialog = false}) async {
    Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) {
                  return _getPage(url, params);
                },
                fullscreenDialog: fullscreenDialog))
        .then((value) => {
              if (callback != null) {callback(value)}
            });
  }

  pushReplacement(BuildContext context, String url, dynamic params) async {
    // url = await Auth().checkPersionMiss(url);

    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
      return _getPage(url, params);
    }));
  }

  pushReplacementNoParams(BuildContext context, String url) async {
    // url = await Auth().checkPersionMiss(url);

    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
      return _getPage(url, {});
    }));
  }

  popNowPage(BuildContext context, {var data = ""}) async {
    Navigator.pop(context, data);
  }

  popUntil(BuildContext context, String url) {
    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) {
      return _getPage(url, {});
    }), (route) => route == null);
  }
}
